const express = require("express");
const path = require("path");
const app = express();
const cors = require("cors");
const joi = require("@hapi/joi");
const config = require("./config/config");
const expressJWT = require("express-jwt");
const querystring = require('querystring');

/* router导入 */
const userRouter = require("./router/userRouter");
const roleRouter = require("./router/roleRouter");
const sadRouter = require("./router/sadRouter");
const usersRouter = require("./router/usersRouter");
const wxchat = require('./router/wechat');
const pages = require('./pages/pages');

app.use(cors());
app.use(express.urlencoded({ extended: false }));
// 解析json中间件
app.use(express.json())

app.use(function (req, res, next) {
  res.cc = function (err, status = 1) {
    res.send({
      status,
      message: err instanceof Error ? err.message : err,
    });
  };
  next();
});
app.use((err, req, res, next) => {
  if (err instanceof joi.ValidationError) return res.cc(err);
  res.cc(err);
});
app.use(
  expressJWT({ secret: config.jwtSecretKey }).unless({ path: [/^\/api/, /^\/$/, /^\/wechat/] })
);
app.use((err, req, res, next) => {
  if (err.name === "UnauthorizedError") return res.cc("身份认证失败");
});

app.use("/", express.static("./public"));
// app.use(express.static(path.join(__dirname, "public")))
app.use("/wechat", wxchat);
app.use("/pages", pages);
app.use("/api", userRouter);
app.use("/role", roleRouter);
app.use("/sad", sadRouter);
app.use("/users", usersRouter);
// app.use("/uploads", express.static("./uploads"));
// app.use("/uploads", express.static(path.join(__dirname, "public")))




app.listen(9000, () => {
  console.log(" ......................阿弥陀佛......................\n" +
    "                       _oo0oo_                      \n" +
    "                      o8888888o                     \n" +
    "                      88\" . \"88                     \n" +
    "                      (| -_- |)                     \n" +
    "                      0\\  =  /0                     \n" +
    "                   ___/‘---’\\___                   \n" +
    "                  .' \\|       |/ '.                 \n" +
    "                 / \\\\|||  :  |||// \\                \n" +
    "                / _||||| -卍-|||||_ \\               \n" +
    "               |   | \\\\\\  -  /// |   |              \n" +
    "               | \\_|  ''\\---/''  |_/ |              \n" +
    "               \\  .-\\__  '-'  ___/-. /              \n" +
    "             ___'. .'  /--.--\\  '. .'___            \n" +
    "         .\"\" ‘<  ‘.___\\_<|>_/___.’>’ \"\".          \n" +
    "       | | :  ‘- \\‘.;‘\\ _ /’;.’/ - ’ : | |        \n" +
    "         \\  \\ ‘_.   \\_ __\\ /__ _/   .-’ /  /        \n" +
    "    =====‘-.____‘.___ \\_____/___.-’___.-’=====     \n" +
    "                       ‘=---=’                      \n" +
    "                                                    \n" +
    "....................佛祖保佑 ,永无BUG...................");
  console.log("http://127.0.0.1:9000");
});
